/**
 * 文件：JuheGridAdapter.java
 * 包名：com.ronri.grid.menuview;   --- 正式发布时需要修正
 * 说明：网格菜单适配器
 * 作者：MS During
 * 日期：2017-04-11
 * 历史：
 */
package com.ronri.menuview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.nongyetongv5.R;

import java.util.ArrayList;
import java.util.List;

public class JuheGridAdapter extends BaseAdapter {

    private Context context;
    private GridView mGv;
    private static int ROW_NUMBER = 2;
    private static int COL_NUMBER = 2;

    /****
     * 菜单映射
     */
    private List<JuheGridItem> lMenu = new ArrayList<JuheGridItem>();



    /****
     * 9宫格初始化
     * @param context
     * @param gv
     */
    public JuheGridAdapter(Context context, GridView gv, List<JuheGridItem> lMenuData) {
        this.context = context;
        lMenu =lMenuData;
        mGv = gv;
    }

    /***
     * 获取数量
     * @return
     */
    @Override
    public int getCount() {
        if (null != lMenu) {
            return lMenu.size();
        } else {
            return 0;
        }
    }

    /***
     * 获取Item
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return lMenu.get(position);
    }

    /***
     * 获取ItemID
     * @param position
     * @return
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /***
     * 获取ItemView
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {

            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.gridview_menu_item, null);

            holder.backcolor = lMenu.get(position).backColor;


            holder.cv = lMenu.get(position).Corner;

            holder.iv = (ImageView) convertView.findViewById(R.id.iv_item);
            holder.iv.setImageBitmap(holder.getImage(lMenu.get(position).Image));// holder.getImageWithCorner(, R.mipmap.corner));

            holder.tv = (TextView) convertView.findViewById(R.id.tv_item);
            //设置标题
            holder.tv.setText(lMenu.get(position).Title);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        //高度计算
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                mGv.getHeight() / ROW_NUMBER);
        convertView.setLayoutParams(param);

        return convertView;
    }

    class Holder {
        ImageView iv;
        TextView tv;
        int backcolor;
        int cv;

        public int getBackcolor(){
            return backcolor;
        }

        public Bitmap getImage(int iiv){
            return BitmapFactory.decodeResource(context.getResources(), iiv);
        }

        public Bitmap getImageWithCorner(int iiv, int icv){

            // 获取目标图片
            Bitmap sourceImage = BitmapFactory.decodeResource(context.getResources(), iiv);
            Bitmap cornerImage = BitmapFactory.decodeResource(context.getResources(), icv);

            int bsWidth = sourceImage.getWidth();
            int bsHeight =sourceImage.getHeight();

            int bcWidth = cornerImage.getWidth();
            int bcHeight = cornerImage.getHeight();

            int newWidth = bsWidth + (bcWidth / 2);
            int newHeight = bsHeight + (bcHeight / 2);

            int[] argb = new int[newWidth * newHeight];

            // 设置透明点
            for (int y = 0; y < newHeight; y++)
                for (int x = 0; x < newWidth; x++)
                {
                    int index = y * newWidth + x;
                    int r = ((argb[index] >> 16) & 0xff)|0xff;
                    int g = ((argb[index] >> 8) & 0xff)|0xff;
                    int b =( argb[index] & 0xff)|0xff;
                    argb[index] = 0xff000000 | (r << 16) | (g << 8) | b;
                }

            // 创建图片(透明)
            //Bitmap targetImage = Bitmap.createBitmap(argb, newWidth, newHeight, Bitmap.Config.ARGB_8888);
            Bitmap targetImage = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
            // 创建画板
            Canvas mCanvas = new Canvas(targetImage);

            // 绘制图标
            mCanvas.drawBitmap(sourceImage, bcWidth / 4, bcHeight / 4, null);

            // 判断是否需要绘制角标，如果需要，则绘制
            if (cv > 0) {
                int pyl = 0;
                String nCv = String.valueOf(cv);
                if (cv > 99){
                    pyl = 13;
                    nCv = "99+";
                }else{
                    if (cv > 10)
                        pyl = 5;
                }

                Paint point = new Paint();
                point.setColor(Color.rgb(255, 255, 255));
                point.setTextSize(24);
                point.setFakeBoldText(true);//setTypeface(Typeface.DEFAULT_BOLD);
                point.setDither(true);
                point.setFilterBitmap(true);

                mCanvas.drawBitmap(cornerImage, bsWidth - bcWidth / 2, bcHeight / 8, null);
                mCanvas.drawText(nCv, bsWidth - bcWidth / 8 - pyl, bcHeight / 2 + 15, point);
            }

            sourceImage.recycle();
            cornerImage.recycle();

            sourceImage = null;
            cornerImage = null;

            return targetImage;
        }
    }
}
